package org.shangrila.bme.mb;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.component.UIInput;
import javax.inject.Inject;
import javax.inject.Named;

import org.primefaces.event.FileUploadEvent;
import org.primefaces.event.SelectEvent;
import org.primefaces.event.ToggleEvent;
import org.shangrila.app.sys.cfg.AppInitInfo;
import org.shangrila.app.sys.shiro.SecUtil;
import org.shangrila.app.wfm.mb.FileUploadProcessor;
import org.shangrila.bme.eo.AppFileInfo;
import org.shangrila.comm.dao.DbAccess;
import org.shangrila.comm.helper.IDHelper;
import org.shangrila.comm.utils.FileUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Named
@SessionScoped
public class AttachUploadMB implements Serializable {
	private static final long serialVersionUID = -5951493223664217112L;
	private final Logger log = LoggerFactory.getLogger(this.getClass());
	@Inject
	private DbAccess db;
	private String operator;
	private String uploadedFile="";
	private String uploadedFileImage="logo_head.jpg";
	private List<AppFileInfo> voList = new ArrayList<AppFileInfo>(); //附件列表
	private AppFileInfo selectedAttach;

	//页面间参数传递
	private UIInput uploadForTypeInput;
	private UIInput uploadForIdInput;
	private UIInput uploadForNameInput;
	
	
	@PostConstruct
    public void init() {
		try{
			operator = SecUtil.getCachedShiroUser().getUserno();
		}
		catch(Exception ex){
			operator="bme";
		}
    }
         
    // handle File Upload
	public void handleFileUpload(FileUploadEvent event) {
		log.info("uploadinfoInput"+uploadForIdInput.getValue().toString());
		
		String fileId = IDHelper.genStrID(24);
		uploadedFile = event.getFile().getFileName();//上传文件路径名
		
		String fileName = uploadedFile.substring(uploadedFile.lastIndexOf(AppInitInfo.WOS_SEPERATOR) + 1);

		String fileExt = FileUtil.getFileExtension(new File(fileName));//上传文件路径名扩展名
		String webfileName = AppInitInfo.BME_UPLOAD_FILE_PATH + fileId +"." + fileExt;
		log.info("webfileName: "+ webfileName);
		
		try {
			FileUploadProcessor.saveInputStreamAsFile(event.getFile().getInputstream(),webfileName);
			//文件存入数据库AppFileInfo
			AppFileInfo afi = new AppFileInfo();
			afi.setId(fileId);
			afi.setForType(uploadForTypeInput.getValue().toString());
			afi.setForId(uploadForIdInput.getValue().toString());
			afi.setForName(uploadForNameInput.getValue().toString());
			afi.setOperator(operator);
			afi.setUpTime(new Date());
			afi.setUpdName(uploadedFile);
			afi.setFilePath(webfileName);
			afi.setFileExt(fileExt);
			//afi.setRemarks(remarks);
			db.begin();
			db.insert(afi);
			db.commit();
			
			uploadedFileImage = fileId+"."+fileExt;		
			log.info(uploadedFile + " is uploaded.");
			voList.add(afi);
			AppInitInfo.showMessage("文件上传", "处理成功");

		} catch (IOException e) {
			AppInitInfo.showMessage("文件上传", "处理失败");
			log.error(uploadedFile + " is uploaded failly.");
		} 		
	}

    public void onToggle(ToggleEvent event) {
    	/*
    	AppInitInfo.showMessage(event.getComponent().getId() + " toggled。","Status:" + event.getVisibility().name());
    	*/
    	if(event.getVisibility().name().equalsIgnoreCase("VISIBLE")) {
			String sql="SELECT m FROM AppFileInfo m WHERE m.forId=?1 and m.forType=?2";
			voList = db.findBySql(sql, new Object[]{uploadForIdInput.getValue().toString(),uploadForTypeInput.getValue().toString()});
    	}
    	else {
    		voList = new ArrayList<AppFileInfo>();
    		uploadedFileImage="";
    	}
    }
    
    public void onRowSelected(SelectEvent event) {
    	selectedAttach = (AppFileInfo)event.getObject();
    	uploadedFileImage = selectedAttach.getId()+"."+selectedAttach.getFileExt();
    	log.info("selectedAttach..." + uploadedFileImage);
    }
    
	public String getUploadedFile() {
		return uploadedFile;
	}

	public void setUploadedFile(String uploadedFile) {
		this.uploadedFile = uploadedFile;
	}

	public String getUploadedFileImage() {
		return uploadedFileImage;
	}

	public void setUploadedFileImage(String uploadedFileImage) {
		this.uploadedFileImage = uploadedFileImage;
	}

	public UIInput getUploadForTypeInput() {
		return uploadForTypeInput;
	}

	public void setUploadForTypeInput(UIInput uploadForTypeInput) {
		this.uploadForTypeInput = uploadForTypeInput;
	}

	public UIInput getUploadForIdInput() {
		return uploadForIdInput;
	}

	public void setUploadForIdInput(UIInput uploadForIdInput) {
		this.uploadForIdInput = uploadForIdInput;
	}

	public UIInput getUploadForNameInput() {
		return uploadForNameInput;
	}

	public void setUploadForNameInput(UIInput uploadForNameInput) {
		this.uploadForNameInput = uploadForNameInput;
	}

	public List<AppFileInfo> getVoList() {
		return voList;
	}

	public void setVoList(List<AppFileInfo> voList) {
		if(voList!=null)
			this.voList = voList;
	}

	public AppFileInfo getSelectedAttach() {
		return selectedAttach;
	}

	public void setSelectedAttach(AppFileInfo selectedAttach) {
		this.selectedAttach = selectedAttach;
	}

}